<?php
/**
 * Some dkret specific widgets that extend the standard WordPress Widgets
 *
 * @package dkret3
 * @since 3.6
 */


/**
 * dkret Double Column widget class
 *
 * @since 3.0
 */
class DKRET_Widget_Sidebar_Columns extends WP_Widget {

    function DKRET_Widget_Sidebar_Columns() {
        $widget_ops = array( 'classname' => 'widget_sidebar_columns', 'description' => __( 'Extra Columns for your Sidebars' , 'dkret' ) );
        $this->WP_Widget( 'dkret-sidebar-columns',  __('Sidebar Columns', 'dkret'), $widget_ops );
    }

    function widget( $args, $instance ) {
        extract( $args );

        echo $before_widget; ?>

        <div id="first-column" class="sidebar sidebar-column" role="complementary">
            <div class="xoxo">
                <?php
                if ( is_active_sidebar( '1st-sidebar-column' ) ) :
                    dynamic_sidebar( '1st-sidebar-column' );
                else :
                    the_widget( 'WP_Widget_Categories', array( 'count' => false, 'hierarchical' => false, 'dropdown' => false ) );
                    the_widget( 'WP_Widget_Archives', array( 'count' => false, 'dropdown' => false ) );
                endif;
                ?>
            </div>
        </div>

        <div id="second-column" class="sidebar sidebar-column" role="complementary">
            <div class="xoxo">
                <?php
                if ( is_active_sidebar( '2nd-sidebar-column' ) ) :
                    dynamic_sidebar( '2nd-sidebar-column' );
                else :
                    the_widget( 'DKRET_Widget_Links', array( 'limit' => '10', 'sortby' => 'rand' ) );
                endif;
                ?>
            </div>
        </div>

        <?php
        echo $after_widget;
    }
    
    function form( $instance ) { ?>
        <p><?php _e( "Drag Widgets to 'Sidebar Columns' Sidebars for customization.", 'dkret' ) ?></p><?php
    }
    
}

/**
 * dkret AdSense widget class
 *
 * @since 3.0
 */
class DKRET_Widget_AdSense extends WP_Widget {

    function DKRET_Widget_AdSense() {
        $widget_ops = array( 'classname' => 'widget_adsene', 'description' => __( 'AdSense Advertisments' , 'dkret' ) );
        $this->WP_Widget( 'dkret-adsense', 'AdSense', $widget_ops );
    }

    function widget( $args, $instance ) {
        extract( $args );

        $title = apply_filters( 'widget_title', $instance['title'] );
        $text = get_option( 'dkret_adsense_block_2' );

        if ( is_user_logged_in() && get_dkret_option( 'hide_adsense' ) )
            return;

        echo $before_widget;
        if ( !empty( $title ) )
            echo $before_title . $title . $after_title; ?>
            
        <div class="adsense-widget"><?php echo $text; ?></div>

        <?php echo $after_widget;
    }

    function update( $new_instance, $old_instance ) {
        $instance = $old_instance;
        $instance['title'] = strip_tags( $new_instance['title'] );

        return $instance;
    }

    function form( $instance ) {
        $instance = wp_parse_args( ( array ) $instance, array( 'title' => '', 'text' => '' ) );
        $title = strip_tags( $instance['title'] ); ?>
        
        <p><label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:' ); ?> <input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" /></label></p>
    
    <?php
    }
}

/**
 * dkret text widget class
 *
 * @since 3.0
 */
class DKRET_Widget_Text extends WP_Widget {

    function DKRET_Widget_Text() {
        $widget_ops = array( 'classname' => 'widget_text', 'description' => __( 'An advanced widget to add arbitrary text or HTML' , 'dkret' ) );
        $control_ops = array( 'width' => 400, 'height' => 350 );
        $this->WP_Widget( 'dkret-text', __( 'Text' ), $widget_ops, $control_ops);
    }

    function widget( $args, $instance ) {
        extract($args);

        $title = apply_filters( 'widget_title', empty( $instance['title'] ) ? '' : $instance['title'], $instance ); 
        $text = apply_filters( 'widget_text', $instance['text'], $instance ); 
        $login = empty( $instance['login'] ) ? '0' : '1';

        if ( is_user_logged_in() && !empty( $login ) )
            return;
        
        echo $before_widget;
        if ( !empty( $title ) ) 
            echo $before_title . $title . $after_title; ?>
        <div class="textwidget"><?php echo $instance['filter'] ? wpautop($text) : $text; ?></div>

        <?php echo $after_widget;
    }

    function update( $new_instance, $old_instance ) {
        $instance = $old_instance;
        $instance['title'] = strip_tags( $new_instance['title'] );
        $instance['login'] = $new_instance['login'] ? '1' : '0';        

        if ( current_user_can( 'unfiltered_html' ) )
            $instance['text'] =  $new_instance['text'];
        else
			$instance['text'] = stripslashes( wp_filter_post_kses( addslashes( $new_instance['text'] ) ) ); // wp_filter_post_kses() expects slashed 

        $instance['filter'] = isset( $new_instance['filter'] );

        return $instance;
    }

    function form( $instance ) {
        $instance = wp_parse_args( (array) $instance, array( 'title' => '', 'text' => '', 'filter' => true, 'login' => false ) );
        $title = strip_tags( $instance['title'] );
        $text = format_to_edit( $instance['text'] );
        $filter = (bool) $instance['filter'];
        $login = (bool) $instance['login'];
        ?>
        <p><label for="<?php echo $this->get_field_id( 'title' ); ?>">
        <?php _e( 'Title:' ); ?>
        <input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" />
        </label></p>

        <p><textarea class="widefat" rows="16" cols="20" id="<?php echo $this->get_field_id( 'text' ); ?>" name="<?php echo $this->get_field_name( 'text' ); ?>"><?php echo $text; ?></textarea></p>

        <p>
            <label for="<?php echo $this->get_field_id( 'login' ); ?>">
                <input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id( 'login' ); ?>" name="<?php echo $this->get_field_name( 'login' ); ?>"<?php checked( $login ); ?> />&nbsp;<?php _e( 'Hide Widget for logged in users.', 'dkret' ); ?>
            </label>
            <br />
            <label for="<?php echo $this->get_field_id( 'filter' ); ?>">
                <input id="<?php echo $this->get_field_id( 'filter' ); ?>" name="<?php echo $this->get_field_name( 'filter' ); ?>" type="checkbox" <?php checked( $instance['filter'] ); ?> />&nbsp;<?php _e( 'Automatically add paragraphs.' ); ?>
            </label>
        </p>
    <?php
    }
}    

/**
 * dkret links widget class
 *
 * @since 3.0
 */
class DKRET_Widget_Links extends WP_Widget {

    function DKRET_Widget_Links() {
        $widget_ops = array( 'classname' => 'widget_links', 'description' => __( 'An advanced widget to add your blogroll', 'dkret' ) );
        $this->WP_Widget( 'dkret-links', __( 'Links' ), $widget_ops );
    }

    function widget( $args, $instance ) {
        extract( $args );

        $title = apply_filters( 'widget_title', empty( $instance['title'] ) ? __( 'Bookmarks' ) : $instance['title'], $instance ); 
        $show_description = isset( $instance['description'] ) ? $instance['description'] : false;
        $show_name = isset( $instance['name'] ) ? $instance['name'] : false;
        $show_rating = isset( $instance['rating'] ) ? $instance['rating'] : false;
        $show_images = isset( $instance['images']) ? $instance['images'] : true;
        $category = isset( $instance['category']) ? $instance['category'] : false;
        $categorize = isset( $instance['categorize']) ? $instance['categorize'] : false;

        $sortby = empty( $instance['sortby'] ) ? 'rand' : $instance['sortby'];
        $limit = empty( $instance['limit'] ) ? '-1' : $instance['limit'];

        if ( is_admin() && ! $category ) :
            // Display All Links widget as such in the widgets screen
            echo $before_widget . $before_title. __('All Links') . $after_title . $after_widget;
            
            return;
        endif;

        $before_widget = preg_replace('/id="[^"]*"/','id="%id"', $before_widget);
        
        wp_list_bookmarks( apply_filters( 'widget_links_args', array(
            'title_before' => $before_title,
            'title_after' => $after_title,
            'title_li' => $title,
            'category_before' => $before_widget,
            'category_after' => $after_widget,
            'show_images' => $show_images,
            'show_description' => $show_description,
            'categorize' => $categorize,
            'orderby' => $sortby,
            'limit' => $limit,
            'show_name' => $show_name,
            'show_rating' => $show_rating,
            'category' => $category,
            'class' => 'linkcat widget' ) )
        );
    }

    function update( $new_instance, $old_instance ) {
        $new_instance = (array) $new_instance;
        $instance = array( 'images' => 0, 'name' => 0, 'description' => 0, 'rating' => 0, 'categorize' => 0 );

        foreach ( $instance as $field => $val ) {
            if ( isset( $new_instance[$field] ) )
                $instance[$field] = 1;
        }
        
        $instance['category'] = intval( $new_instance['category'] );

        if ( in_array( $new_instance['sortby'], array( 'rand', 'ID', 'url', 'name', 'rating', 'length' ) ) ) :
            $instance['sortby'] = $new_instance['sortby'];
        else :
            $instance['sortby'] = 'rand';
        endif;
        
        $instance['limit'] = $new_instance['limit'];
        $instance['title'] = strip_tags( $new_instance['title'] );
        
        return $instance;
    }

    function form( $instance ) {
        $instance = wp_parse_args( (array) $instance, array( 'title' => __( 'Bookmarks' ), 'images' => true, 'name' => true, 'description' => false, 'rating' => false, 'category' => false, 'categorize' => false, 'sortyby' => 'rand', 'limit' => '' ) );
        $link_cats = get_terms( 'link_category' );
        $title = strip_tags( $instance['title'] );
        
        $limit = $instance['limit'];
        ?>
        <p><label for="<?php echo $this->get_field_id( 'title' ); ?>">
        <?php _e( 'Title:' ); ?>
        <input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" />
        </label></p>

        <p>
            <label for="<?php echo $this->get_field_id( 'category' ); ?>"><?php _e( 'Limit to category:', 'dkret' ); ?>
                <select class="widefat" id="<?php echo $this->get_field_id( 'category' ); ?>" name="<?php echo $this->get_field_name( 'category' ); ?>">
                    <option value=""><?php _e( 'All Links' ); ?></option>
                    <?php
                    foreach ( $link_cats as $link_cat ) {
                        echo '<option value="' . esc_attr( intval($link_cat->term_id) ) . '"'
                            . ( $link_cat->term_id == $instance['category'] ? ' selected="selected"' : '' )
                            . '>' . $link_cat->name . "</option>\n";
                    }
                    ?>
                </select>
            </label>
        </p>
        <p>
            <label for="<?php echo $this->get_field_id( 'sortby' ); ?>"><?php _e( ' Sort by: ' ); ?>
                <select name="<?php echo $this->get_field_name( 'sortby' ); ?>" id="<?php echo $this->get_field_id( 'sortby' ); ?>" class="widefat">
                    <option value="rand"<?php selected( $instance['sortby'], 'rand' ); ?>><?php _e( 'Random Order' ); ?></option>
                    <option value="ID"<?php selected( $instance['sortby'], 'ID' ); ?>><?php _e( 'ID' ); ?></option>
                    <option value="url"<?php selected( $instance['sortby'], 'url' ); ?>><?php _e( 'URL' ); ?></option>
                    <option value="name"<?php selected( $instance['sortby'], 'name' ); ?>><?php _e( 'Name' ); ?></option>
                    <option value="rating"<?php selected( $instance['sortby'], 'rating' ); ?>><?php _e( 'Rating' ); ?></option>
                    <option value="length"<?php selected( $instance['sortby'], 'length' ); ?>><?php _e( 'Length' ); ?></option>
                </select>
            </label>
        </p>
        <p><label for="<?php echo $this->get_field_id( 'limit' ); ?>">
        <?php _e( 'Number of Links to show:', 'dkret' ); ?>
        <input id="<?php echo $this->get_field_id( 'limit' ); ?>" name="<?php echo $this->get_field_name( 'limit' ); ?>" type="number" value="<?php echo esc_attr( $limit ) ?>" size="3" /></label>
        <br /><small><?php _e( '(Leave free to show all Links)', 'dkret' ); ?></small></p>
        <p>
            <label for="<?php echo $this->get_field_id( 'images' ); ?>"><input class="checkbox" type="checkbox" <?php checked( $instance['images'], true ); ?> id="<?php echo $this->get_field_id( 'images' ); ?>" name="<?php echo $this->get_field_name( 'images' ); ?>" />&nbsp;<?php _e( 'Show Link Image' ); ?></label><br />
            <label for="<?php echo $this->get_field_id( 'name' ); ?>"><input class="checkbox" type="checkbox" <?php checked( $instance['name'], true ); ?> id="<?php echo $this->get_field_id( 'name' ); ?>" name="<?php echo $this->get_field_name( 'name' ); ?>" />&nbsp;<?php _e( 'Show Link Name' ); ?></label><br />
            <label for="<?php echo $this->get_field_id( 'description' ); ?>"><input class="checkbox" type="checkbox" <?php checked( $instance['description'], true ); ?> id="<?php echo $this->get_field_id( 'description' ); ?>" name="<?php echo $this->get_field_name( 'description' ); ?>" />&nbsp;<?php _e( 'Show Link Description' ); ?></label><br />
            <label for="<?php echo $this->get_field_id( 'rating' ); ?>"><input class="checkbox" type="checkbox" <?php checked( $instance['rating'], true ); ?> id="<?php echo $this->get_field_id( 'rating' ); ?>" name="<?php echo $this->get_field_name( 'rating' ); ?>" />&nbsp;<?php _e( 'Show Link Rating' ); ?></label><br />
            <label for="<?php echo $this->get_field_id( 'categorize' ); ?>"><input class="checkbox" type="checkbox" <?php checked( $instance['categorize'], true ); ?> id="<?php echo $this->get_field_id( 'categorize' ); ?>" name="<?php echo $this->get_field_name( 'categorize' ); ?>" />&nbsp;<?php _e( 'Show Categories', 'dkret' ); ?></label>
        </p>
    <?php
    }
}

/**
 * dkret meta widget class
 *
 * @since 3.0
 */
class DKRET_Widget_Meta extends WP_Widget {

    function DKRET_Widget_Meta() {
        $widget_ops = array( 'classname' => 'widget_meta', 'description' => __( 'An advanced widget to add log in/out, admin, feed and WordPress links', 'dkret' ) );
        $this->WP_Widget( 'dkret-meta', __('Meta'), $widget_ops );
    }

    function widget( $args, $instance ) {
        extract( $args );
        $title = apply_filters( 'widget_title', $instance['title'] );      
        $login = empty( $instance['login'] ) ? '0' : '1';
        $rss_feed = empty( $instance['rss_feed'] ) ? '0' : '1';
        global $user_identity, $user_ID;

        echo $before_widget;
        
        if ( !empty( $title ) && empty( $user_identity ) )
            echo $before_title . $title . $after_title;
            
        elseif ( !empty( $user_identity ) )
            echo $before_title . $user_identity . $after_title;

        if ( !is_user_logged_in() && !empty( $login ) ) :
            wp_register('<ul><li>','</li></ul>');
            wp_login_form();
        else :
            $dkret_avatar_size = apply_filters( 'dkret_avatar_size', '40' ); // Filter for changing avatar size
            echo get_avatar( $user_ID, $dkret_avatar_size ); ?>

            <ul>
                <?php wp_register(); ?>
                <li><?php wp_loginout( get_bloginfo( 'url' ) ); ?></li>
                <?php
                    if ( empty( $rss_feed ) ) :
                        wp_meta();
                    endif;
                ?>
            </ul>
        
        <?php    
        endif;
        
        if ( !empty( $rss_feed ) ) : ?>
        
            <ul class="meta_rss">
                <li><a href="<?php bloginfo( 'rss2_url' ); ?>" title="<?php echo esc_attr( __( 'Syndicate this site using RSS 2.0' ) ); ?>"><?php _e( 'Entries <abbr title="Really Simple Syndication">RSS</abbr>' ); ?></a></li>
                <li><a href="<?php bloginfo( 'comments_rss2_url' ); ?>" title="<?php echo esc_attr( __( 'The latest comments to all posts in RSS' ) ); ?>"><?php _e( 'Comments <abbr title="Really Simple Syndication">RSS</abbr>' ); ?></a></li>
                <?php wp_meta(); ?>
            </ul>
        <?php
        endif;
       
        echo $after_widget;
    }

    function update( $new_instance, $old_instance ) {
        $instance = $old_instance;
        $instance['title'] = strip_tags( $new_instance['title'] );
        $instance['login'] = $new_instance['login'] ? 1 : 0;
        $instance['rss_feed'] = $new_instance['rss_feed'] ? 1 : 0;
        
        return $instance;
    }

    function form( $instance ) {
        $instance = wp_parse_args( (array) $instance, array( 'title' => __( 'Login/Register', 'dkret' ), 'login' => true, 'rss_feed' => false ) );
        $title = strip_tags( $instance['title'] );
        $login = (bool) $instance['login'];
        $rss_feed = (bool) $instance['rss_feed'];
        ?>
        <p><label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:' ); ?> <input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" /></label></p>
        <p>
            <label for="<?php echo $this->get_field_id( 'login' ); ?>">
                <input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id( 'login' ); ?>" name="<?php echo $this->get_field_name( 'login' ); ?>"<?php checked( $login ); ?> />&nbsp;<?php _e( 'Login on Posts page', 'dkret' ); ?>
            </label>
            <br />
            <label for="<?php echo $this->get_field_id( 'rss_feed' ); ?>">
                <input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id( 'rss_feed' ); ?>" name="<?php echo $this->get_field_name( 'rss_feed' ); ?>"<?php checked( $rss_feed ); ?> />&nbsp;<?php _e( 'Show RSS-Feeds', 'dkret' ); ?>
            </label>
        </p>
    <?php
    }
}    

/**
 * dkret pages widget class
 *
 * @since 3.0
 */
class DKRET_Widget_Pages extends WP_Widget {

    function DKRET_Widget_Pages() {
        $widget_ops = array( 'classname' => 'widget_pages', 'description' => __( "An advanced widget to add your blog's WordPress Pages", 'dkret' ) );
        $this->WP_Widget( 'dkret-pages', __('Pages'), $widget_ops );
    }

    function widget( $args, $instance ) {
        extract( $args );
        global $post;
        
        $title = apply_filters( 'widget_title', $instance['title'] );  
        $sortby = empty( $instance['sortby'] ) ? 'menu_order' : $instance['sortby'];
        $exclude = empty( $instance['exclude'] ) ? '' : $instance['exclude'];
        $show_all = $instance['show_all'] ? '1' : '0';
        $fold = $instance['fold'] ? '1' : '0';

        if ( $sortby == 'menu_order' )
            $sortby = 'menu_order, post_title';

        if( $post->post_parent )
            $children = wp_list_pages( array( 'title_li' => '', 'child_of' => $post->post_parent, 'echo' => '0' ) );
        
        else
            $children = wp_list_pages( array( 'title_li' => '', 'child_of' => $post->ID, 'echo' => '0' ) );

        if ( $children || $show_all ) :

            echo $before_widget;

            if ( $title )
                echo $before_title . $title . $after_title;

            if ( $fold )
                echo "<ul class=\"fold-menu\">";
            
            else
                echo "<ul>";
            
            wp_list_pages( apply_filters( 'widget_pages_args', array(
                'sort_column' => $sortby,
                'exclude' => $exclude,
                'title_li' => '' ) ) 
            );
            
            echo "</ul>";
            echo $after_widget;
        
        endif;
    }

    function update( $new_instance, $old_instance ) {
        $instance = $old_instance;
        $instance['title'] = strip_tags( $new_instance['title'] );

        if ( in_array( $new_instance['sortby'], array( 'post_title', 'menu_order', 'ID' ) ) )
            $instance['sortby'] = $new_instance['sortby'];
        
        else
            $instance['sortby'] = 'menu_order';

        $instance['exclude'] = strip_tags( $new_instance['exclude'] );
        $instance['show_all'] = $new_instance['show_all'] ? 1 : 0;
        $instance['fold'] = $new_instance['fold'] ? 1 : 0;
        
        return $instance;
    }

    function form( $instance ) {
        //Defaults
        $instance = wp_parse_args( (array) $instance, array( 'show_all' => true, 'fold' => true, 'sortby' => 'post_title', 'title' => __( 'Pages' ), 'exclude' => '' ) );
        $title = esc_attr( $instance['title'] );
        $exclude = esc_attr( $instance['exclude'] );
        $show_all = (bool) $instance['show_all'];
        $fold = (bool) $instance['fold'];
        ?>
        <p><label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:' ); ?> <input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $title ) ?>" /></label></p>
        <p>
            <label for="<?php echo $this->get_field_id( 'show_all' ); ?>">
                <input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id( 'show_all' ); ?>" name="<?php echo $this->get_field_name( 'show_all' ); ?>"<?php checked( $show_all ); ?> />
                <?php _e( 'Show Navigation on all pages', 'dkret' ); ?>
            </label>
            <br />
                <label for="<?php echo $this->get_field_id( 'fold' ); ?>">
                    <input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id( 'fold' ); ?>" name="<?php echo $this->get_field_name( 'fold' ); ?>"<?php checked( $fold ); ?> />
                    <?php _e( 'Hide subpages', 'dkret' ); ?>
                </label>
        </p>
        <p>
            <label for="<?php echo $this->get_field_id( 'sortby' ); ?>"><?php _e( 'Sort by:' ); ?>
                <select name="<?php echo $this->get_field_name( 'sortby' ); ?>" id="<?php echo $this->get_field_id( 'sortby' ); ?>" class="widefat">
                    <option value="post_title"<?php selected( $instance['sortby'], 'post_title' ); ?>><?php _e( 'Page title' ); ?></option>
                    <option value="menu_order"<?php selected( $instance['sortby'], 'menu_order' ); ?>><?php _e( 'Page order' ); ?></option>
                    <option value="ID"<?php selected( $instance['sortby'], 'ID' ); ?>><?php _e( 'Page ID' ); ?></option>
                </select>
            </label>
        </p>
        <p>
            <label for="<?php echo $this->get_field_id( 'exclude' ); ?>"><?php _e( 'Exclude:' ); ?>
                <input type="text" value="<?php echo esc_attr( $exclude ) ?>" name="<?php echo $this->get_field_name( 'exclude' ); ?>" id="<?php echo $this->get_field_id( 'exclude' ); ?>" class="widefat" />
            </label>
            <br />
            <small><?php _e( 'Page IDs, separated by commas.' ); ?></small>
        </p>
    <?php
    }

}

/**
 * dkret Navigation Menu widget class
 *
 * @since 3.6
 */
 class DKRET_Nav_Menu_Widget extends WP_Widget {

	function DKRET_Nav_Menu_Widget() {
		$widget_ops = array( 'description' => __('An advanced Widget to add one of your custom menus as a widget.', 'dkret') );
		parent::WP_Widget( 'dkret_nav_menu', __('Custom Menu'), $widget_ops );
	}

	function widget($args, $instance) {
		// Get menu
		$nav_menu = wp_get_nav_menu_object( $instance['nav_menu'] );

        $fold = isset( $instance['fold'] ) ? $instance['fold'] : false;
        
		if ( !$nav_menu )
			return;

		echo $args['before_widget'];

		if ( !empty($instance['title']) )
			echo $args['before_title'] . $instance['title'] . $args['after_title'];

        if ( !$fold )        
            wp_nav_menu( array( 'fallback_cb' => '', 'menu' => $nav_menu, 'menu_class' => 'menu' ) );
        else
            wp_nav_menu( array( 'fallback_cb' => '', 'menu' => $nav_menu, 'menu_class' => 'fold-menu' ) );

		echo $args['after_widget'];
	}

	function update( $new_instance, $old_instance ) {
		$instance['title'] = strip_tags( stripslashes($new_instance['title']) );
		$instance['nav_menu'] = (int) $new_instance['nav_menu'];
        $instance['fold'] = (bool) $new_instance['fold'] ? true : false;
		return $instance;
	}

	function form( $instance ) {
		$title = isset( $instance['title'] ) ? $instance['title'] : '';
		$nav_menu = isset( $instance['nav_menu'] ) ? $instance['nav_menu'] : '';
        $fold = isset( $instance['fold'] ) ? $instance['fold'] : false;
        
		// Get menus
		$menus = get_terms( 'nav_menu', array( 'hide_empty' => false ) );

		// If no menus exists, direct the user to go and create some.
		if ( !$menus ) {
			echo '<p>'. sprintf( __('No menus have been created yet. <a href="%s">Create some</a>.'), admin_url('nav-menus.php') ) .'</p>';
			return;
		}
		?>
		<p>
			<label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:') ?></label>
			<input type="text" class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" value="<?php echo $title; ?>" />
		</p>
		<p>
			<label for="<?php echo $this->get_field_id('nav_menu'); ?>"><?php _e('Select Menu:'); ?></label>
			<select id="<?php echo $this->get_field_id('nav_menu'); ?>" name="<?php echo $this->get_field_name('nav_menu'); ?>">
		<?php
			foreach ( $menus as $menu ) {
				$selected = $nav_menu == $menu->term_id ? ' selected="selected"' : '';
				echo '<option'. $selected .' value="'. $menu->term_id .'">'. $menu->name .'</option>';
			}
		?>
			</select>
		</p>
        <p>
            <label for="<?php echo $this->get_field_id( 'fold' ); ?>">
                <input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id( 'fold' ); ?>" name="<?php echo $this->get_field_name( 'fold' ); ?>"<?php checked( $fold ); ?> />
                <?php _e( 'Hide subpages', 'dkret' ); ?>
            </label>
        </p>
		<?php
	}
}

/**
 * dkret rss links widget class
 *
 * @since 3.0
 */
class DKRET_Widget_RSS_Links extends WP_Widget {

    function DKRET_Widget_RSS_Links() {
        $widget_ops = array( 'classname' => 'widget_rsslinks', 'description' => __( 'RSS links for posts/comments' , 'dkret' ) );
        $this->WP_Widget( 'dkret-rss-links', __('RSS Links', 'dkret' ), $widget_ops );
    }    

    function widget( $args, $instance ) {
        extract( $args );

        $title = apply_filters( 'widget_title', $instance['title'] );
        $posts = $instance['posts'] ? '1' : '0';
        $comments = $instance['comments'] ? '1' : '0';

        echo $before_widget;
        if ( $title )
            echo $before_title . $title . $after_title; ?>

        <ul>

        <?php if ( ! $posts ) : ?>
            <li><a href="<?php bloginfo( 'rss2_url' ); ?>" title="<?php echo esc_attr( __( 'Syndicate this site using RSS 2.0' ) ); ?>" rel="alternate" type="application/rss+xml"><?php _e( 'All posts', 'dkret' ); ?></a></li>
        <?php endif; ?>
        
        <?php if ( ! $comments ) : ?>
            <li><a href="<?php bloginfo( 'comments_rss2_url' ); ?>" title="<?php echo esc_attr( __( 'The latest comments to all posts in RSS' ) ); ?>" rel="alternate" type="application/rss+xml"><?php _e( 'All comments', 'dkret'); ?></a></li>
        <?php endif; ?>

        </ul>

        <?php echo $after_widget;
    }

    function update( $new_instance, $old_instance ) {
        $instance = $old_instance;
        $instance['title'] = strip_tags($new_instance['title'] );
        $instance['posts'] = $new_instance['posts'] ? 1 : 0;
        $instance['comments'] = $new_instance['comments'] ? 1 : 0;

        return $instance;
    }

    function form( $instance ) {
        //Defaults
        $instance = wp_parse_args( (array) $instance, array( 'title' => __( 'RSS Links', 'dkret' ), 'posts' => false, 'comments' => false ) );
        $title = esc_attr( $instance['title'] );
        $posts = (bool) $instance['posts'];
        $comments = (bool) $instance['comments'];
        ?>
        <p><label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:' ); ?> <input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" /></label></p>
        <p>
            <label for="<?php echo $this->get_field_id( 'posts' ); ?>">
                <input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id( 'posts' ); ?>" name="<?php echo $this->get_field_name( 'posts' ); ?>"<?php checked( $posts ); ?> />
                <?php _e( 'Hide Posts RSS feed' , 'dkret' ); ?>
            </label>
            <br />
            <label for="<?php echo $this->get_field_id( 'comments' ); ?>">
                <input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id( 'comments' ); ?>" name="<?php echo $this->get_field_name( 'comments' ); ?>"<?php checked( $comments ); ?> />&nbsp;<?php _e( 'Hide Comments RSS feed' , 'dkret' ); ?>
            </label>
        </p>
    <?php
    }
}

/**
 * dkret Calendar widget class
 *
 * @since 3.0
 */
class DKRET_Widget_Calendar extends WP_Widget {

    function DKRET_Widget_Calendar() {
        $widget_ops = array( 'classname' => 'widget_calendar', 'description' => __( 'An advanced widget to add a calendar of your blog&#8217;s posts', 'dkret' ) );
        $this->WP_Widget( 'dkret-calendar', __( 'Calendar' ), $widget_ops );
    }

    function widget( $args, $instance ) {
        extract( $args );

        $title = apply_filters( 'widget_title', $instance['title'] );    
        echo $before_widget;
        
        if ( $title )
            echo $before_title . $title . $after_title; ?>
            
        <div id="calendar_wrap">

            <?php get_calendar(); ?>
        
        </div>
        
        <?php echo $after_widget;
    }

    function update( $new_instance, $old_instance ) {
        $instance = $old_instance;
        $instance['title'] = strip_tags( $new_instance['title'] );

        return $instance;
    }

    function form( $instance ) {
        $instance = wp_parse_args( (array) $instance, array( 'title' => __( 'Calendar' ) ) );
        $title = strip_tags( $instance['title'] ); ?>
        
        <p>
            <label for="<?php echo $this->get_field_id( 'title' ); ?>">
                <?php _e( 'Title:' ); ?>
                <input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" />
            </label>
        </p>
        
    <?php
    }
}
?>